<?php
/*
 * Created on 22.5.2006
 */
 
class pStandard_Checklist extends cFormElement 
{
	
	function getShowText($fieldName, $id, $value, cView $view)
	{
		if ($view->doctype=="raw") 
		{
			if ($value instanceof SimpleXMLElement) 
			{
				foreach ($value->children() as $name=>$el) {
					$s[]=$name;
				} 
				return implode(",",$s);
			}
		} else return $value;
	}
	
	function getEditText($fieldName, $id, $value, cView $view)
	{
		$checked = explode(",",$value);
		$choices = $this->prepareChoices($view);
		$s.="<fieldset class=\"checklist\"><legend><label for=\"$id\">".$this->getAttr("label")."</label></legend>";
		foreach ($choices as $key=>$ch) {
			if (in_array($ch["val"],$checked)) {
				$sel="checked=\"checked\""; 
			} else $sel="";	
			$s.="<input type=\"checkbox\" name=\"".$fieldName."[".$ch["val"]."]\" $sel id=\"".$id."_".$ch["val"]."\" /><label for=\"".$id."_".$ch["val"]."\" />".$ch["title"]."</label><br />";
		}
		$s.="</fieldset>";

		return $s;
	}
	
	function prepareChoices($view)
	{
		$ch = array();
		$keywords = array("children", "subnodes", "types");
		if (in_array($rel=$this->getAttr("between"),$keywords))
		{
			if ($rel=="types") {
				$info = $this->cms->file->listTypes();
				foreach ($info as $group=>$types) {
					foreach ($types as $type=>$name) {
						$ch[]=array("title"=>$name,"val"=>$type);
					}
				}
			} else if ($rel=="children") {
				$choices = $this->node->getChildren($view);
				foreach ($choices as $choice) {
					$ch[]=array("title"=>$choice->name,"val"=>$choice->id);
				}
			} else if ($rel=="subnodes") {	
				$choices = $this->node->getSubNodes($view);
				foreach ($choices as $choice) {
					$ch[]=array("title"=>$choice->name,"val"=>$choice->id);
				}
			}					

		} else {

			if (($b = $this->getAttr("between"))!='') 
			{
				$choices = explode(",",$b);
				foreach ($choices as $k=>$choice) {
					$chunks=explode(":",$choice);
					$ch[] = array("title"=>$chunks[1],"val"=>$chunks[0]);
				}
			} else if (($select = $this->getAttr("select"))!='') 
			{
				$node = $this->cms->tree->getNodes($select);
				foreach ($node as $n) 
				{
					$ch[] = array("title"=>$n->name,"val"=>$n->id);
				}
			}
			
		}		
		if (($e=$this->getAttr("empty"))!="") {
			$ch=array_reverse($ch);
			$ch[]=array("title"=>$e, "val"=>"NULL");
			$ch=array_reverse($ch);
		}			
		return $ch; 
	}
	
}
 
?>